package com.example.chenzhiran.myapplication7.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.Statement;

public class mainPage {

    private Socket socket;
    private clientData indata;
    private serverData outdata = new serverData();
    Statement statement;
    ObjectOutputStream out;

    public mainPage(Socket mysocket, clientData mydata, Statement stmt)
    {
        this.socket = mysocket;
        this.indata = mydata;
        this.statement = stmt;

        switch(indata.dataId)
        {
            case 0:
                judgeCategory();
                break;
            case 1:
                refreshPage();
                break;
        }

        try
        {
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(outdata);
            out.close();
            System.out.printf("主页数据已发送\n");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void judgeCategory()
    {
        String pic;
        if(indata.gendere == true)
        {
            pic = "malepic";
        }
        else
        {
            pic = "femalepic";
        }
        getImg(pic, 0);
    }

    public void refreshPage()
    {
        String pic;
        if(indata.category.equals("0") || indata.category.equals("1"))
        {
            pic = "malepic";
        }
        else
        {
            pic = "femalepic";
        }
        getImg(pic, indata.picpos);
    }

    public void getImg(String gender, int pos)
    {
        String sql = "select count(1) as totalCount from " +  gender;
        int totalnum = 0;
        try {
            ResultSet numresult = statement.executeQuery(sql);
            while(numresult.next())
            {
                totalnum = numresult.getInt("totalCount");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        if(pos + 4 >= totalnum)
        {
            outdata.datastatus = 1;
            return ;
        }

        sql = "select * from " + gender + " limit " + String.valueOf(pos) + ", 4";
        try {
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String path = rs.getString("loc");
                String describe = rs.getString("des");
                String sprice = rs.getString("pri");
                outdata.productdescribes.put(path,describe);
                outdata.productprices.put(path, Integer.parseInt(sprice));

                FileInputStream fin = new FileInputStream(new File(path));
                byte[] pic = new byte[fin.available()];
                fin.read(pic);
                outdata.imgs.put(path, pic);
                fin.close();
                outdata.datastatus = 0;
            }
            rs.close();
        }catch (Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }
    }
}
